package com.changyou.kf.mock.controller;

import com.alibaba.fastjson2.JSON;
import com.changyou.kf.mock.entity.RoleInfo;
import com.changyou.kf.mock.service.GameLoginService;
import com.changyou.kf.mock.socket.CustomWebSocketHandler;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/api/websocket")
@RestController
public class WebSocketController {

    @Resource
    private GameLoginService gameLoginService;

    @Resource
    private CustomWebSocketHandler customWebSocketHandler;

    @PostMapping("/broadcast")
    public ResponseEntity<String> broadcastMessage(@RequestBody String message) {
        customWebSocketHandler.broadcastMessage(message);
        return ResponseEntity.ok("消息广播成功");
    }

    @GetMapping("/role_info")
    public ResponseEntity<String> getAppkeyRoleInfoMap() {
        Map<String, RoleInfo> getRoleInfoMap = gameLoginService.getRoleInfoMap();
        String data = JSON.toJSONString(getRoleInfoMap);
        return ResponseEntity.ok(data);
    }

    @PostMapping("/game_chat")
    public ResponseEntity<String> gameChat(@RequestBody Map<String, String> params) {
        //customWebSocketHandler.broadcastMessage(message);
        log.info("gameChat params:{}", JSON.toJSONString(params));

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "succ");

        String tenantId = params.getOrDefault("tenantId", "");
        String appkey = params.getOrDefault("appkey", "");
        String serverId = params.getOrDefault("serverId", "");
        String roleId = params.getOrDefault("roleId", "");
        String message = params.getOrDefault("message", "");
        String targetType = params.getOrDefault("targetType", "");
        String targetId = params.getOrDefault("targetId", "");

        String roleName = String.format("角色名(%s)", roleId);
        RoleInfo roleInfo = gameLoginService.getRoleInfo(roleId);
        log.info("gameChat roleId:{},roleInfo:{}", roleId, JSON.toJSONString(roleInfo));
        if (roleInfo == null) {
            result.put("code", 1);
            result.put("message", "玩家未在线");
            return ResponseEntity.ok(JSON.toJSONString(result));
        }
        if (StringUtils.isNotEmpty(roleInfo.getRoleName())) {
            roleName = roleInfo.getRoleName();
        }

        boolean ret = customWebSocketHandler.sendGameChat(roleId, message);
        if (!ret) {
            result.put("code", 2);
            result.put("message", "玩家未连接");
            return ResponseEntity.ok(JSON.toJSONString(result));
        }
        gameLoginService.writeChatMessage(appkey, serverId, roleId, roleName, message, targetType, targetId);
        return ResponseEntity.ok(JSON.toJSONString(result));
    }
}
